package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.LimitEntity;
import com.forlove.app.qselect.login.service.IChoiceLimitService;
import com.forlove.app.qselect.login.service.impl.ChoiceLimitServiceImpl;
import com.forlove.app.qselect.login.view.ChoiceLimitView;

import java.util.Map;

/**
 * Created by yjl on 2017/10/22.
 */

public class ChoiceLimitPresenter extends IBasePresenter {

    private IChoiceLimitService choiceLimitService;
    private ChoiceLimitView choiceLimitView;

    public ChoiceLimitPresenter(ChoiceLimitView choiceLimitView) {
        if (choiceLimitService == null) {
            choiceLimitService = new ChoiceLimitServiceImpl();
            setService(choiceLimitService);
        }
        this.choiceLimitView = choiceLimitView;
    }


    public void loadLimitData(Map param) {
        if (param == null) {
            return;
        }
        choiceLimitService.loadData(ServerManager.ACTION_LIMIT_LOCATION, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LimitEntity limitEntity = ParseHelper.getModel(result, "data", LimitEntity.class);
                LogUtil.e("yjl", "运营限制:" + result);
                if (choiceLimitView != null) {
                    choiceLimitView.updateLimit(limitEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (choiceLimitView != null) {
                        choiceLimitView.loadFail(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (choiceLimitView != null) {
                        choiceLimitView.loadFail(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
